﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsOtherStockOutDAL.cs
/// 作  者：SAM
/// 时  间：2006.11.23
/// 说  明：ClsOtherStockOutDAL类用于其他出库信息相关处理
/// </summary>
public class ClsOtherStockOutDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有其他入库或其他出库主表信息
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回采购入库或退货主表信息数据集</returns>
    public DataSet GetOtherStockOutMains(int Tm_Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GettbhTranM", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@tm_type", Tm_Type);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有其他入库或其他出库信息
    /// <param name="No">ID号</param>
    /// </summary>
    /// <returns>返回采购入库或退货从表信息数据集</returns>
    public DataSet GetOtherStockOutChilds(string Td_Type, string Tm_No)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GettbhTranD", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@tm_no", Tm_No);
        dat.SelectCommand.Parameters.Add("@td_type", Td_Type);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 改变单的状态
    /// </summary>
    /// <param name="Tm_No">盘点单单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateOtherStockOutState(string Tm_No, string Tm_Type, int State, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatetbhTranMState", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = Tm_Type;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = State;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;


        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废子单
    /// </summary>
    /// <param name="Tm_No"></param>
    /// <param name="Td_Type"></param>
    /// <param name="Im_No"></param>
    /// <param name="State"></param>
    /// <returns></returns>
    public Boolean DeleteChildOtherStockOut(string Tm_No, string Td_Type, string Im_No, int State)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeletetbhTranD", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@td_type", SqlDbType.NVarChar, 2);
        com.Parameters["@td_type"].Value = Td_Type;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int);
        com.Parameters["@dm_state"].Value = State;


        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加销售退货信息
    /// </summary>
    /// <param>销售退货编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    /// strDm_no,strDm_date,strCt_name,strDm_money,strDm_Cancel,strDm_Stop,strDm_Writer,strCr_Remark
    public Boolean AddOtherStockOut(string Tm_Type, string Tm_No, string Dp_Id, string Whi_Id, string Wk_Id, string Hk_Id, string Oper, string Remark, int OperateType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddtbhTranM", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = Tm_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_id"].Value = Dp_Id;
        com.Parameters.Add("@whi_id", SqlDbType.NVarChar, 20);
        com.Parameters["@whi_id"].Value = Whi_Id;
        com.Parameters.Add("@wk_id", SqlDbType.NVarChar, 20);
        com.Parameters["@wk_id"].Value = Wk_Id;
        com.Parameters.Add("@hk_id", SqlDbType.NVarChar, 20);
        com.Parameters["@hk_id"].Value = Hk_Id;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 60);
        com.Parameters["@oper"].Value = Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;
        com.Parameters.Add("@operate_type", SqlDbType.Int);
        com.Parameters["@operate_type"].Value = OperateType;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    public Boolean AddOrUpdateOtherStockOut(string Td_Type, string Tm_No, string Im_No, string Remark, int OperateType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddtbhTranD", con);

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@td_type", SqlDbType.NVarChar, 2);
        com.Parameters["@td_type"].Value = Td_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;
        com.Parameters.Add("@operate_type", SqlDbType.Int);
        com.Parameters["@operate_type"].Value = OperateType;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    ///修改
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateOtherStockOut(string sTm_type, string sTm_No, DateTime dtInto_date, string sInto_type, string sTm_dept, string sTm_Oper, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatetbhTranM_OtherOut", con);//Pro_UpdatetbhTranMState  Pro_UpdatetbhTranM_M
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;
        com.Parameters.Add("@Into_date", SqlDbType.DateTime, 8);
        com.Parameters["@Into_date"].Value = dtInto_date;
        com.Parameters.Add("@Into_type", SqlDbType.NVarChar, 30);
        com.Parameters["@Into_type"].Value = sInto_type;
        com.Parameters.Add("@Tm_dept", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_dept"].Value = sTm_dept;
        com.Parameters.Add("@Tm_Oper", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_Oper"].Value = sTm_Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;
        com.Parameters.Add("@Cr_id", SqlDbType.NVarChar, 30);//共用同一个SP,此SP中有这个参数
        com.Parameters["@Cr_id"].Value = "";

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}